﻿using System.Collections.ObjectModel;
using System.IO;
using Agnes.Core;
using System.Xml.Linq;
using Caliburn.Micro;

namespace Agnes.MyDocsLib
{
    /// <summary>
    /// 文章管理器
    /// </summary>
    public class ArticleManager : PropertyChangedBase
    {
        private static ArticleManager _instance = null;

        private ArticleManager()
        {
            _documentBasePath = Path.Combine(AgnesServiceManager.AgnesSlnService.CurrentSln.TempDir, "Date_MyDocsLib");
            _articlesXml = Path.Combine(_documentBasePath, "Articles.xml");

            Load();
        }

        /// <summary>
        /// 唯一实例
        /// </summary>
        public static ArticleManager Instance
        {
            get
            {
                if (AgnesServiceManager.AgnesSlnService.CurrentSln == null)
                    return null;
                if (_instance == null)
                    _instance = new ArticleManager();
                return _instance;
            }
        }
        
        private readonly string _documentBasePath;

        private readonly string _articlesXml;

        /// <summary>
        /// 文章树的根节点
        /// </summary>
        public ObservableCollection<Catalog> Roots { get; private set; }

        /// <summary>
        /// 加载文章树
        /// </summary>
        public void Load()
        {
            Roots = new ObservableCollection<Catalog>();
            if (File.Exists(_articlesXml))
            {
                XElement el = XElement.Load(_articlesXml);
                foreach (XElement i in el.Elements("Catalog"))
                    Roots.Add(Catalog.FetchAsChild(i));
            }
        }

        /// <summary>
        /// 保存文章树
        /// </summary>
        public void Save()
        {
            if (!Directory.Exists(_documentBasePath))
            {
                Directory.CreateDirectory(_documentBasePath);
            }
            if (!File.Exists(_articlesXml))
            {
                File.Create(_articlesXml).Close();
            }
            XElement el = new XElement("Articles");
            foreach (Catalog i in Roots)
            {
                i.SaveToXml(t => el.Add(t));
            }
            el.Save(_articlesXml);
        }



        private IArticleNode _selectedNode = null;
        /// <summary>
        /// 选择的文章树的节点
        /// </summary>
        public IArticleNode SelectedNode 
        {
            get
            {
                return _selectedNode;
            }
            set
            {
                _selectedNode = value;
                //调整命令
                NotifyOfPropertyChange(() => SelectedNode);
            }
        }

        public void DelCatalog(Catalog catalog)
        {
            if (catalog.ParentCatalog != null)
            {
                catalog.Delete();
            }
            else
            {
                Roots.Remove(catalog);
            }
        }

        public void DelArticle(Article article)
        {
            if (File.Exists(article.DocumentPath))
            {
                File.Delete(article.DocumentPath);
            }

            article.ParentCatalog.Childs.Remove(article);
        }
    }

}
